<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="原文：Four common types of code coverage 作者：Ramona Schwering, Jecelyn Yeen  您听说过“代码覆盖率”吗？在这篇文章中，我们将探讨什么是测试中的代码覆盖率，以及四种衡量它的常用方法。">
<meta property="og:type" content="article">
<meta property="og:title" content="四种常见的代码覆盖率测试">
<meta property="og:url" content="https://wyatex.online/JavaScript/%E5%9B%9B%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E6%B5%8B%E8%AF%95/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="原文：Four common types of code coverage 作者：Ramona Schwering, Jecelyn Yeen  您听说过“代码覆盖率”吗？在这篇文章中，我们将探讨什么是测试中的代码覆盖率，以及四种衡量它的常用方法。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-09-15T10:00:00.000Z">
<meta property="article:modified_time" content="2023-09-15T10:01:13.941Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="测试">
<meta property="article:tag" content="代码覆盖率">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/JavaScript/%E5%9B%9B%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E6%B5%8B%E8%AF%95/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/JavaScript/%E5%9B%9B%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E6%B5%8B%E8%AF%95/","path":"JavaScript/四种常见的代码覆盖率测试/","title":"四种常见的代码覆盖率测试"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>四种常见的代码覆盖率测试 | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">1.</span> <span class="nav-text">什么是代码覆盖率</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.</span> <span class="nav-text">四种常见的代码覆盖类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">2.1.</span> <span class="nav-text">函数覆盖率</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A1%8C%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">2.2.</span> <span class="nav-text">行覆盖率</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E6%94%AF%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">2.3.</span> <span class="nav-text">分支覆盖率</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">2.4.</span> <span class="nav-text">语句覆盖率</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%82%A8%E5%BA%94%E8%AF%A5%E9%80%89%E6%8B%A9%E5%93%AA%E7%A7%8D%E7%B1%BB%E5%9E%8B%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%EF%BC%9F"><span class="nav-number">3.</span> <span class="nav-text">您应该选择哪种类型的代码覆盖率？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95%E8%A6%86%E7%9B%96%E7%8E%87%E4%B8%8E%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E7%9B%B8%E5%90%8C%E5%90%97"><span class="nav-number">4.</span> <span class="nav-text">测试覆盖率与代码覆盖率相同吗</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#100-%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E5%B9%B6%E4%B8%8D%E6%84%8F%E5%91%B3%E7%9D%80%E6%B2%A1%E6%9C%89bug"><span class="nav-number">5.</span> <span class="nav-text">100%的代码覆盖率并不意味着没有bug</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0100-%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E7%9A%84%E6%AF%AB%E6%97%A0%E6%84%8F%E4%B9%89%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">5.1.</span> <span class="nav-text">实现100%代码覆盖率的毫无意义的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E4%B8%AA%E7%B3%9F%E7%B3%95%E7%9A%84%E5%BA%A6%E9%87%8F%E6%A0%87%E5%87%86%E6%AF%94%E6%B2%A1%E6%9C%89%E5%BA%A6%E9%87%8F%E6%A0%87%E5%87%86%E6%9B%B4%E7%B3%9F%E7%B3%95"><span class="nav-number">5.2.</span> <span class="nav-text">一个糟糕的度量标准比没有度量标准更糟糕</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9C%A8%E4%B8%8D%E5%90%8C%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%B5%8B%E8%AF%95%E4%B8%AD%E4%BD%BF%E7%94%A8%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87"><span class="nav-number">6.</span> <span class="nav-text">在不同类型的测试中使用代码覆盖率</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">7.</span> <span class="nav-text">总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/JavaScript/%E5%9B%9B%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E6%B5%8B%E8%AF%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="四种常见的代码覆盖率测试 | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          四种常见的代码覆盖率测试
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2023-09-15 18:00:00 / 修改时间：18:01:13" itemprop="dateCreated datePublished" datetime="2023-09-15T18:00:00+08:00">2023-09-15</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote>
<p>原文：<a target="_blank" rel="noopener" href="https://web.dev/ta-code-coverage/">Four common types of code coverage</a></p>
<p>作者：<a target="_blank" rel="noopener" href="https://web.dev/authors/ramona/">Ramona Schwering</a>, <a target="_blank" rel="noopener" href="https://web.dev/authors/ramona/">Jecelyn Yeen</a></p>
</blockquote>
<p>您听说过“代码覆盖率”吗？在这篇文章中，我们将探讨什么是测试中的代码覆盖率，以及四种衡量它的常用方法。</p>
<span id="more"></span>

<h2 id="什么是代码覆盖率"><a href="#什么是代码覆盖率" class="headerlink" title="什么是代码覆盖率"></a>什么是代码覆盖率</h2><p>代码覆盖率是衡量测试代码测试了源代码百分比多少的指标。它可以帮助您识别可能缺乏适当测试的代码区域。</p>
<p>通常，覆盖率指标会这样去记录：</p>
<table>
<thead>
<tr>
<th>File</th>
<th>% Statements</th>
<th>% Branch</th>
<th>% Functions</th>
<th>% Lines</th>
<th>Uncovered lines</th>
</tr>
</thead>
<tbody><tr>
<td>file.js</td>
<td>90%</td>
<td>100%</td>
<td>90%</td>
<td>80%</td>
<td>89,256</td>
</tr>
<tr>
<td>coffee.js</td>
<td>55.55%</td>
<td>80%</td>
<td>50%</td>
<td>62.5%</td>
<td>10-11, 18</td>
</tr>
</tbody></table>
<p>当您添加新的功能和测试时，增加代码覆盖率百分比可以让您更加确信您的应用程序已经经过了彻底的测试。然而，还有更多的问题有待发现。</p>
<h2 id="四种常见的代码覆盖类型"><a href="#四种常见的代码覆盖类型" class="headerlink" title="四种常见的代码覆盖类型"></a>四种常见的代码覆盖类型</h2><p>有四种常见的方法来收集和计算代码覆盖率:函数、行、分支和语句覆盖率。要查看每种类型的代码覆盖率如何计算其百分比，请思考以下计算咖啡成分的代码示例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">calcCoffeeIngredient</span>(<span class="params">coffeeName, cup = <span class="number">1</span></span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> espresso, water;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;espresso&#x27;</span>) &#123;</span><br><span class="line">    espresso = <span class="number">30</span> * cup;</span><br><span class="line">    <span class="keyword">return</span> &#123; espresso &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;americano&#x27;</span>) &#123;</span><br><span class="line">    espresso = <span class="number">30</span> * cup; water = <span class="number">70</span> * cup;</span><br><span class="line">    <span class="keyword">return</span> &#123; espresso, water &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">isValidCoffee</span>(<span class="params">name</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> [<span class="string">&#x27;espresso&#x27;</span>, <span class="string">&#x27;americano&#x27;</span>, <span class="string">&#x27;mocha&#x27;</span>].<span class="title function_">includes</span>(name);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>不是很懂英语，去查了一下分别是：espresso-浓缩咖啡，americano-美式咖啡，mocha-摩卡咖啡</p>
</blockquote>
<p>验证<code>calcCoffeeIngredient</code>函数的测试是</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.test.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; describe, expect, assert, it &#125; <span class="keyword">from</span> <span class="string">&#x27;vitest&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; calcCoffeeIngredient &#125; <span class="keyword">from</span> <span class="string">&#x27;../src/coffee-incomplete&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="title function_">describe</span>(<span class="string">&#x27;Coffee&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;should have espresso&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> result = <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;espresso&#x27;</span>, <span class="number">2</span>);</span><br><span class="line">    <span class="title function_">expect</span>(result).<span class="property">to</span>.<span class="property">deep</span>.<span class="title function_">equal</span>(&#123; <span class="attr">espresso</span>: <span class="number">60</span> &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;should have nothing&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> result = <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;unknown&#x27;</span>);</span><br><span class="line">    <span class="title function_">expect</span>(result).<span class="property">to</span>.<span class="property">deep</span>.<span class="title function_">equal</span>(&#123;&#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>您可以在此<a target="_blank" rel="noopener" href="https://stackblitz.com/edit/vitest-coffee-coverage?file=README.md">demo</a>中运行代码和测试，也可以<a target="_blank" rel="noopener" href="https://github.com/leichteckig/vitest-coffee-example">签出存储库</a>。</p>
<h3 id="函数覆盖率"><a href="#函数覆盖率" class="headerlink" title="函数覆盖率"></a>函数覆盖率</h3><p><strong>代码覆盖率:50%</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">calcCoffeeIngredient</span>(<span class="params">coffeeName, cup = <span class="number">1</span></span>) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">isValidCoffee</span>(<span class="params">name</span>) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>功能覆盖率是一个简单的指标。它表示计算出测试代码调用了源代码中百分之多少函数。</p>
<p>在代码示例中，有两个函数:<code>calcCoffeeIngredient</code>和<code>isValidCoffee</code>。测试代码只调用<code>calcCoffeeIngredient</code>函数，因此函数覆盖率为50%。</p>
<h3 id="行覆盖率"><a href="#行覆盖率" class="headerlink" title="行覆盖率"></a>行覆盖率</h3><p><strong>代码覆盖率:62.5%</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">calcCoffeeIngredient</span>(<span class="params">coffeeName, cup = <span class="number">1</span></span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> espresso, water;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;espresso&#x27;</span>) &#123; <span class="comment">// 1</span></span><br><span class="line">    espresso = <span class="number">30</span> * cup;  <span class="comment">// 2</span></span><br><span class="line">    <span class="keyword">return</span> &#123; espresso &#125;;  <span class="comment">// 3</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;americano&#x27;</span>) &#123;  <span class="comment">// 4</span></span><br><span class="line">    espresso = <span class="number">30</span> * cup; water = <span class="number">70</span> * cup; <span class="comment">// 5</span></span><br><span class="line">    <span class="keyword">return</span> &#123; espresso, water &#125;;  <span class="comment">// 6</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;&#125;;  <span class="comment">// 7</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">isValidCoffee</span>(<span class="params">name</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> [<span class="string">&#x27;espresso&#x27;</span>, <span class="string">&#x27;americano&#x27;</span>, <span class="string">&#x27;mocha&#x27;</span>].<span class="title function_">includes</span>(name);  <span class="comment">// 8</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>行覆盖率表示测试代码覆盖源代码的可执行代码行的百分比。如果一行代码仍然未执行，这意味着代码的某些部分没有经过测试。</p>
<p>代码示例有8行可执行代码，但是测试不执行<code>americano</code>条件(两行)和<code>isValidCoffee</code>函数(一行)。这使得线路覆盖率达到62.5%。</p>
<h3 id="分支覆盖率"><a href="#分支覆盖率" class="headerlink" title="分支覆盖率"></a>分支覆盖率</h3><p><strong>代码覆盖率:80%</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">calcCoffeeIngredient</span>(<span class="params">coffeeName, cup = <span class="number">1</span></span>) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;espresso&#x27;</span>) &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="keyword">return</span> &#123; espresso &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;americano&#x27;</span>) &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">    <span class="keyword">return</span> &#123; espresso, water &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line">…</span><br></pre></td></tr></table></figure>

<p>分支覆盖率表示代码中执行分支或决策点的百分比，例如if语句或循环。它测定测试是否检查条件语句为true和false的分支。</p>
<p>代码示例中有五个分支：</p>
<ol>
<li>只使用<code>coffeeName</code>调用<code>calccoffeingredient</code> √</li>
<li>用<code>coffeeName</code>和<code>cup</code>调用<code>calcCoffeeIngredient</code> √</li>
<li><code>coffeeName</code> 是 浓缩咖啡 √</li>
<li><code>coffeeName</code> 是 美式 ×</li>
<li>其他咖啡 √</li>
</ol>
<p>测试涵盖除了咖啡是美式咖啡条件所有分支，所以分支覆盖率是80%。</p>
<h3 id="语句覆盖率"><a href="#语句覆盖率" class="headerlink" title="语句覆盖率"></a>语句覆盖率</h3><p><strong>代码覆盖率:55.55%</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">calcCoffeeIngredient</span>(<span class="params">coffeeName, cup = <span class="number">1</span></span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> espresso, water;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;espresso&#x27;</span>) &#123;</span><br><span class="line">    espresso = <span class="number">30</span> * cup;</span><br><span class="line">    <span class="keyword">return</span> &#123; espresso &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;americano&#x27;</span>) &#123;</span><br><span class="line">    espresso = <span class="number">30</span> * cup; water = <span class="number">70</span> * cup;</span><br><span class="line">    <span class="keyword">return</span> &#123; espresso, water &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">isValidCoffee</span>(<span class="params">name</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> [<span class="string">&#x27;espresso&#x27;</span>, <span class="string">&#x27;americano&#x27;</span>, <span class="string">&#x27;mocha&#x27;</span>].<span class="title function_">includes</span>(name);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>语句覆盖率检测测试代码执行了代码中百分之几的语句。乍一看，您可能会想，这与线路覆盖不一样吗？实际上，语句覆盖类似于行覆盖，但考虑的是包含多个语句的单行代码。</p>
<p>在代码示例中，有8行可执行代码，但是有9条语句。你能找出包含两个语句的行吗？</p>
<blockquote>
<p>答案揭晓：一行有两个语句的代码：<code>espresso = 30 * cup; water = 70 * cup;</code></p>
</blockquote>
<p>测试只覆盖了9条语句中的5条，因此语句覆盖率为55.55%。</p>
<p>如果您总是每行写一条语句，那么您的行覆盖率将与语句覆盖率相似。</p>
<h2 id="您应该选择哪种类型的代码覆盖率？"><a href="#您应该选择哪种类型的代码覆盖率？" class="headerlink" title="您应该选择哪种类型的代码覆盖率？"></a>您应该选择哪种类型的代码覆盖率？</h2><p>大多数代码覆盖率测试工具包括这四种类型的通用代码覆盖率。选择哪个代码覆盖指标来确定优先级取决于具体的项目需求、开发实践和测试目标。</p>
<p>通常，语句覆盖率是一个很好的起点，因为它是一个简单且易于理解的指标。与语句覆盖率不同，分支覆盖率和函数覆盖率指标的是测试是调用条件(分支)还是函数。因此，它们是语句覆盖之后才应该考虑的。</p>
<p>一旦您获得了较高的语句覆盖率，您就可以继续进行提高分支覆盖率和函数覆盖率。</p>
<h2 id="测试覆盖率与代码覆盖率相同吗"><a href="#测试覆盖率与代码覆盖率相同吗" class="headerlink" title="测试覆盖率与代码覆盖率相同吗"></a>测试覆盖率与代码覆盖率相同吗</h2><p>不一样。测试覆盖率和代码覆盖率经常被混淆，但它们是不同的：</p>
<ul>
<li>测试覆盖率:一种度量测试套件覆盖软件特性的程度的定性度量。它有助于确定所涉及的风险水平。</li>
<li>代码覆盖率:一种量化的度量，用于度量在测试期间执行的代码的比例。它是关于测试覆盖了多少代码。</li>
</ul>
<p>这里有一个简单的类比:把一个web应用程序想象成一个房子。</p>
<ul>
<li>测试覆盖率衡量房子里覆盖了多少房间的程度。</li>
<li>代码覆盖率衡量了测试了多少房子。</li>
</ul>
<h2 id="100-的代码覆盖率并不意味着没有bug"><a href="#100-的代码覆盖率并不意味着没有bug" class="headerlink" title="100%的代码覆盖率并不意味着没有bug"></a>100%的代码覆盖率并不意味着没有bug</h2><p>虽然在测试中实现高代码覆盖率当然是可取的，但100%的代码覆盖率并不能保证代码中没有错误或缺陷。</p>
<h3 id="实现100-代码覆盖率的毫无意义的方法"><a href="#实现100-代码覆盖率的毫无意义的方法" class="headerlink" title="实现100%代码覆盖率的毫无意义的方法"></a>实现100%代码覆盖率的毫无意义的方法</h3><p>参考下面的测试：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.test.js */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ...</span></span><br><span class="line"><span class="title function_">describe</span>(<span class="string">&#x27;Warning: Do not do this&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;is meaningless&#x27;</span>, <span class="function">() =&gt;</span> &#123; </span><br><span class="line">    <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;espresso&#x27;</span>, <span class="number">2</span>);</span><br><span class="line">    <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;americano&#x27;</span>);</span><br><span class="line">    <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;unknown&#x27;</span>);</span><br><span class="line">    <span class="title function_">isValidCoffee</span>(<span class="string">&#x27;mocha&#x27;</span>);</span><br><span class="line">    <span class="title function_">expect</span>(<span class="literal">true</span>).<span class="title function_">toBe</span>(<span class="literal">true</span>); <span class="comment">// not meaningful assertion</span></span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>这个测试实现了100%的函数、行、分支和语句覆盖率，但是它没有意义，因为它实际上并没有测试代码。无论代码是否正常工作，<code>expect(true).tobe(true)</code>断言都会通过测试。</p>
<h3 id="一个糟糕的度量标准比没有度量标准更糟糕"><a href="#一个糟糕的度量标准比没有度量标准更糟糕" class="headerlink" title="一个糟糕的度量标准比没有度量标准更糟糕"></a>一个糟糕的度量标准比没有度量标准更糟糕</h3><p>一个糟糕的指标会给你一种虚假的安全感，这比根本没有指标更糟糕。例如，如果您有一个达到100%代码覆盖率的测试代码，但是所有的测试都是无意义的，那么您可能会有一种错误的安全感，认为您的代码已经经过了很好的测试。如果不小心删除或破坏了应用程序代码的一部分，即使应用程序不再正常工作，测试仍然会通过。</p>
<p>要避免这种情况：</p>
<ul>
<li>测试评估：编写和审查测试，以确保它们是有意义的，并在各种不同的场景中测试代码。</li>
<li>使用代码覆盖率作为指导方针，而不是作为测试有效性或代码质量的唯一度量。</li>
</ul>
<h2 id="在不同类型的测试中使用代码覆盖率"><a href="#在不同类型的测试中使用代码覆盖率" class="headerlink" title="在不同类型的测试中使用代码覆盖率"></a>在不同类型的测试中使用代码覆盖率</h2><p>让我们仔细看看如何使用<a target="_blank" rel="noopener" href="https://web.dev/ta-what-to-test/#test-specifics-dos-and-donts">三种常见类型的测试</a>的代码覆盖率：</p>
<ul>
<li>单元测试。它们是收集代码覆盖率的最佳测试类型，因为它们被设计为覆盖多个小场景和测试路径。</li>
<li>集成测试。它们可以帮助收集集成测试的代码覆盖率，但是要谨慎使用。在这种情况下，您计算了源代码的大部分的覆盖率，并且很难确定哪些测试实际上覆盖了代码的哪些部分。尽管如此，计算集成测试的代码覆盖率对于没有良好隔离单元的遗留系统可能是有用的。</li>
<li>端到端(E2E)测试。由于这些测试的复杂性，测量E2E测试的代码覆盖率是困难和具有挑战性的。所以不应该使用代码覆盖，更好的方法是选择需求覆盖。这是因为E2E测试的重点是覆盖测试的需求，而不是关注源代码。</li>
</ul>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>代码覆盖率是衡量测试有效性的有用指标。通过确保代码中的关键逻辑经过良好测试，它可以帮助您提高应用程序的质量。</p>
<p>然而，请记住代码覆盖率只是一个度量标准。确保还要考虑其他因素，例如测试的质量和应用程序需求。</p>
<p>100%的代码覆盖率不是目标。相反，您应该使用代码覆盖以及一个包含各种测试方法的全面测试计划，包括单元测试、集成测试、端到端测试。</p>
<p>请参阅完整的代码示例，并使用良好的代码覆盖率进行测试。您还可以使用这个<a target="_blank" rel="noopener" href="https://stackblitz.com/edit/vitest-coffee-coverage?file=README.md">demo</a>运行代码和测试。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.js - a complete example */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">calcCoffeeIngredient</span>(<span class="params">coffeeName, cup = <span class="number">1</span></span>) &#123;</span><br><span class="line">  <span class="keyword">if</span> (!<span class="title function_">isValidCoffee</span>(coffeeName)) <span class="keyword">return</span> &#123;&#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">let</span> espresso, water;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;espresso&#x27;</span>) &#123;</span><br><span class="line">    espresso = <span class="number">30</span> * cup;</span><br><span class="line">    <span class="keyword">return</span> &#123; espresso &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (coffeeName === <span class="string">&#x27;americano&#x27;</span>) &#123;</span><br><span class="line">    espresso = <span class="number">30</span> * cup; water = <span class="number">70</span> * cup;</span><br><span class="line">    <span class="keyword">return</span> &#123; espresso, water &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span> (<span class="string">`<span class="subst">$&#123;coffeeName&#125;</span> not found`</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">isValidCoffee</span>(<span class="params">name</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> [<span class="string">&#x27;espresso&#x27;</span>, <span class="string">&#x27;americano&#x27;</span>, <span class="string">&#x27;mocha&#x27;</span>].<span class="title function_">includes</span>(name);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* coffee.test.js - a complete test suite */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; describe, expect, it &#125; <span class="keyword">from</span> <span class="string">&#x27;vitest&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; calcCoffeeIngredient &#125; <span class="keyword">from</span> <span class="string">&#x27;../src/coffee-complete&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="title function_">describe</span>(<span class="string">&#x27;Coffee&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;should have espresso&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> result = <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;espresso&#x27;</span>, <span class="number">2</span>);</span><br><span class="line">    <span class="title function_">expect</span>(result).<span class="property">to</span>.<span class="property">deep</span>.<span class="title function_">equal</span>(&#123; <span class="attr">espresso</span>: <span class="number">60</span> &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;should have americano&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> result = <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;americano&#x27;</span>);</span><br><span class="line">    <span class="title function_">expect</span>(result.<span class="property">espresso</span>).<span class="property">to</span>.<span class="title function_">equal</span>(<span class="number">30</span>);</span><br><span class="line">    <span class="title function_">expect</span>(result.<span class="property">water</span>).<span class="property">to</span>.<span class="title function_">equal</span>(<span class="number">70</span>);</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;should throw error&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> <span class="title function_">func</span> = (<span class="params"></span>) =&gt; <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;mocha&#x27;</span>);</span><br><span class="line">    <span class="title function_">expect</span>(func).<span class="title function_">toThrowError</span>(<span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&#x27;mocha not found&#x27;</span>));</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="title function_">it</span>(<span class="string">&#x27;should have nothing&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> result = <span class="title function_">calcCoffeeIngredient</span>(<span class="string">&#x27;unknown&#x27;</span>)</span><br><span class="line">    <span class="title function_">expect</span>(result).<span class="property">to</span>.<span class="property">deep</span>.<span class="title function_">equal</span>(&#123;&#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/JavaScript/%E5%9B%9B%E7%A7%8D%E5%B8%B8%E8%A7%81%E7%9A%84%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E6%B5%8B%E8%AF%95/" title="四种常见的代码覆盖率测试">https://wyatex.online/JavaScript/四种常见的代码覆盖率测试/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag"># 测试</a>
              <a href="/tags/%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87/" rel="tag"># 代码覆盖率</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/Rust/rust-build-js-runtime-2/" rel="prev" title="用 Rust 构建你自己的 JavaScript 运行时实践（2）">
                  <i class="fa fa-angle-left"></i> 用 Rust 构建你自己的 JavaScript 运行时实践（2）
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/%E5%89%8D%E7%AB%AF/QwikCourse-1/" rel="next" title="Qwik官方入门教程（1）">
                  Qwik官方入门教程（1） <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
